package com.ruoyi.fly.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.fly.domain.*;
import com.ruoyi.fly.domain.vo.ServerOrderSearchVo;
import com.ruoyi.fly.domain.vo.ServerOrderVo;
import com.ruoyi.fly.enums.ServerStatusType;
import com.ruoyi.fly.mapper.*;
import com.ruoyi.fly.service.ServerOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ServerOrderServiceImpl implements ServerOrderService {

    @Autowired
    private ServerOrderMapper serverOrderMapper;

    @Autowired
    private VehiclesMapper vehiclesMapper;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private AppletServerMapper appletServerMapper;
    @Autowired
    private AppletMemberMapper appletMemberMapper;


    /**
     * 根据id查询服务订单
     * @param id
     * @return
     */
    @Override
    public ServerOrderVo selectAppletServerOrderById(String id) {
        ServerOrderVo serverOrderVo = new ServerOrderVo();
        ServerOrder serverOrder = serverOrderMapper.selectAppletServerOrderById(id);
        Vehicles vehicles = vehiclesMapper.selectPcVehiclesById(serverOrder.getVehiclesId());
        Store store = storeMapper.selectStoreById(serverOrder.getStoreId());
        AppletServer appletServer = appletServerMapper.selectAppletServerById(serverOrder.getServerId());
        BeanUtils.copyProperties(serverOrder, serverOrderVo);
        serverOrderVo.setLicensePlate(vehicles.getLicensePlate());
        serverOrderVo.setStoreName(store.getStoreName());
        serverOrderVo.setTitle(appletServer.getTitle());
        return serverOrderVo;
    }

    /**
     * 查询服务订单列表
     * @param serverOrderSearchVo
     * @return
     */
    @Override
    public List<ServerOrderVo> selectAppletServerOrderList(ServerOrderSearchVo serverOrderSearchVo) {
        List<ServerOrderVo> serverOrderVos = serverOrderMapper.selectAppletServerOrderList(serverOrderSearchVo);
        serverOrderVos.forEach(serverOrderVo -> {
            if (storeMapper.selectStoreById(serverOrderVo.getStoreId()) != null) {
                serverOrderVo.setStoreName(storeMapper.selectStoreById(serverOrderVo.getStoreId()).getStoreName());
            }
            if (vehiclesMapper.selectPcVehiclesById(serverOrderVo.getVehiclesId()) != null) {
                serverOrderVo.setLicensePlate(vehiclesMapper.selectPcVehiclesById(serverOrderVo.getVehiclesId()).getLicensePlate());
            }
            if (appletServerMapper.selectAppletServerById(serverOrderVo.getServerId()) != null){
                serverOrderVo.setTitle(appletServerMapper.selectAppletServerById(serverOrderVo.getServerId()).getTitle());
            }
            if (appletMemberMapper.selectAppletMemberById(serverOrderVo.getMid()) != null){
                serverOrderVo.setNickName(appletMemberMapper.selectAppletMemberById(serverOrderVo.getMid()).getNickname());
            }
            if(appletMemberMapper.selectAppletMemberById(serverOrderVo.getMid()) != null){
                serverOrderVo.setNickName(appletMemberMapper.selectAppletMemberById(serverOrderVo.getMid()).getNickname());
            }
            serverOrderVo.setServerStatusValue(ServerStatusType.getValue(Integer.valueOf(serverOrderVo.getServerStatus())));
        });
        return serverOrderVos;
    }

    /**
     * 新增服务订单
     * @param serverOrder
     * @return
     */
    @Override
    public R insertAppletServerOrder(ServerOrder serverOrder) {
        serverOrder.setCreateUser(String.valueOf(SecurityUtils.getUserId()));
        return serverOrderMapper.insertAppletServerOrder(serverOrder) > 0 ? R.ok("新增成功") : R.fail("操作失败");
    }

    /**
     * 修改服务订单
     * @param serverOrder
     * @return
     */
    @Override
    public R updateAppletServerOrder(ServerOrder serverOrder) {
        serverOrder.setUpdateUser(String.valueOf(SecurityUtils.getUserId()));
        return serverOrderMapper.updateAppletServerOrder(serverOrder) > 0 ? R.ok("修改成功") : R.fail("操作失败");
    }

    /**
     * 删除服务订单
     * @param id
     * @return
     */
    @Override
    public R deleteAppletServerOrderById(String id) {
        return serverOrderMapper.deleteAppletServerOrderById(id) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }

    @Override
    public R deleteAppletServerOrderByIds(String[] id) {
        return serverOrderMapper.deleteAppletServerOrderByIds(id) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }
}
